package no18.javaio;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * 存储和恢复数据
 */
public class StoringAndRecoveringData {
	private final static String OUT_FILE_NAME = "D:/work/project/complex/complex.thinkinjava/src/main/java/no18/javaio/output.txt";
	
	public static void main(String[] args) throws IOException{
		//建立与目标输出文件的通道
		DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(OUT_FILE_NAME)));
		out.writeDouble(3.14159);
		out.writeUTF("This was pi");
		out.writeDouble(1.41413);
		out.writeUTF("Square root of 2");
		out.close();
		
		//建立与目标输入文件的通道
		DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(OUT_FILE_NAME)));
		in.mark(10000);//打个标记
		while((in.available() !=0)){
			System.out.print((char)in.readByte());
		}
		
		in.reset();//恢复到标记位置
		System.out.println();
		System.out.println(in.readDouble());
		System.out.println(in.readUTF());
		System.out.println(in.readDouble());
		System.out.println(in.readUTF());
		in.close();
		
	}
}
